package com.mypog;

import java.util.HashMap;
import java.util.Map;

public class AllUsers {

	// Stored user objects.
	public static Map<String, User> accounts = new HashMap<String, User>();

	/**
	 * User object search.
	 * 
	 * @param name
	 *            current player name
	 * @param userOrEnemy
	 *            needed to search for user or enemy. "user" to search for
	 *            current user. "enemy" to get ready and free enemy.
	 * @return found User object or NULL.
	 */
	public User getUserObject(String name, String userOrEnemy) {
		// User object
		if (userOrEnemy.equals("user")) {
			return accounts.get(name);
			
			// Free enemy object
		} else if (userOrEnemy.equals("enemy")) {
			for(User user : accounts.values()){
				if (!user.getName().equals(name)) {
					if (user.isReady() && user.getEnemy().equals("")) {
						return user;
					}
				}
			}				
		}
		return null;
	}

	// Return playing user enemy object.
	public User playingUsersEnemy(String name) {
		for (User user : accounts.values()) {

			// User enemy object
			if (user.getEnemy().equals(name)) {
				return user;
			}
		}
		return null;
	}
}
